package cc.icourt.kit.util

import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.view.View
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.annotation.NonNull
import androidx.appcompat.content.res.AppCompatResources
import androidx.core.content.ContextCompat
import cc.icourt.kit.KitApplication
import com.blankj.utilcode.util.SizeUtils

/**
 * -----------------------------------------------
 * 作    者：高延荣
 * 电    话：18963580395
 * 创建日期：2022/03/21 14:20
 * 描    述：
 * 修订历史：
 * -----------------------------------------------
 */

fun getColorYL(@ColorRes id: Int): Int {
    return ContextCompat.getColor(KitApplication.mInstance, id)
}

fun getDrawableYL(@DrawableRes resId: Int): Drawable? {
   return AppCompatResources.getDrawable(KitApplication.mInstance, resId)
}

/**
 * 设置圆角，边框线及颜色，背景颜色
 *
 * @param view        控件
 * @param radiusDP    半径，单位dp
 * @param strokeDP    边框，单位dp
 * @param strokeColor 边框颜色，R.color
 * @param solidColor  填充色
 */
fun setRadiusShape(
    view: View,
    radiusDP: Float,
    strokeDP: Float,
    @ColorRes strokeColor: Int,
    @ColorRes solidColor: Int
) {
    val drawable = GradientDrawable()
    drawable.cornerRadius = SizeUtils.dp2px(radiusDP).toFloat()
    drawable.setStroke(SizeUtils.dp2px(strokeDP), getColorYL(strokeColor))
    drawable.setColor(getColorYL(solidColor))
    view.background = drawable
}

fun setRadiusShape(
    view: View,
    radii: FloatArray?,
    strokeDP: Float,
    @ColorRes strokeColor: Int,
    @ColorRes solidColor: Int
) {
    val drawable = GradientDrawable()
    drawable.cornerRadii = radii
    drawable.setStroke(SizeUtils.dp2px(strokeDP), getColorYL(strokeColor))
    drawable.setColor(getColorYL(solidColor))
    view.background = drawable
}


/**
 * 设置圆角，边框线及颜色
 *
 * @param view
 * @param radiusDP
 * @param strokeDP
 * @param strokeColor
 */
fun setRadiusShape(view: View, radiusDP: Float, strokeDP: Float, @ColorRes strokeColor: Int) {
    val drawable = GradientDrawable()
    drawable.cornerRadius = SizeUtils.dp2px(radiusDP).toFloat()
    drawable.setStroke(SizeUtils.dp2px(strokeDP), getColorYL(strokeColor))
    view.background = drawable
}

fun setRadiusShape(view: View, radii: FloatArray?, strokeDP: Float, @ColorRes strokeColor: Int) {
    val drawable = GradientDrawable()
    drawable.cornerRadii = radii
    drawable.setStroke(SizeUtils.dp2px(strokeDP), getColorYL(strokeColor))
    view.background = drawable
}

/**
 * 设置圆角，背景色
 *
 * @param view
 * @param radiusDP
 * @param solidColor
 */
fun setRadiusShape(view: View, radiusDP: Float, @ColorRes solidColor: Int) {
    val drawable = GradientDrawable()
    drawable.cornerRadius = SizeUtils.dp2px(radiusDP).toFloat()
    drawable.setColor(getColorYL(solidColor))
    view.background = drawable
}

fun setRadiusShape(view: View, radii: FloatArray?, @ColorRes solidColor: Int) {
    val drawable = GradientDrawable()
    radii?.forEachIndexed { index, it ->
        radii[index] = SizeUtils.dp2px(it).toFloat()
    }
    drawable.cornerRadii = radii
    drawable.setColor(getColorYL(solidColor))
    view.background = drawable
}

